package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;

public class GetAllElectricityMetersThatAreNotWrittenOffCommand implements Command
{
	private Logger logger = Logger.getLogger(GetAllElectricityMetersThatAreNotWrittenOffCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		List<ElectricityMeter> listOfElectricityMeters = null;
		ElectricityMeterService electricityMeterService = null;
		
		try 
		{
			electricityMeterService = new ElectricityMeterServiceImpl();
			listOfElectricityMeters = electricityMeterService.getAllElectricityMetersThatAreNotWrittenOff();
			request.setAttribute("listOfElectricityMeters", listOfElectricityMeters);
			operationResult = OPERATION_SUCCESSFUL;
				
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}